package com.itheima.datastructure.stack;

import java.util.LinkedList;

/**
 * 双栈模拟队列
 * ⚪调用push、pop 等方法的次数最多为100
 */
public class E04Leetcode232 {
    /*
        队列头     队列尾

        顶   底   底   顶
        s1            s2

     */
    LinkedList<Integer> s1 = new LinkedList<>();
    LinkedList<Integer> s2 = new LinkedList<>();

    public void push(int x){ //向队列尾添加
        s2.push(x);
    }
    public int pop(){ // 向队列头移除
        if (s1.isEmpty()) {
            while(!s2.isEmpty()){
                s1.push(s2.pop());
            }
        }
        return s1.pop();
    }
    public int peek(){ // 从队列头获取
        if (s1.isEmpty()) {
            while(!s2.isEmpty()){
                s1.push(s2.pop());
            }
        }
        return s1.peek();
    }
    public boolean empty(){
        return s1.isEmpty() && s2.isEmpty();
    }
}
